package SqlJavaBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MakeTestBean {
	private Connection con;
	
	public MakeTestBean(Connection c){
		con = c;
	}
	public boolean makeExam(){
		return false;
	}
	public boolean makePart(){
		return false;
	}
	public String createAnExam(String name,String stime,String etime,int whenLong,int counts){ //将创建的一场考试的详细信息记录下来
		Timestamp startTime;
		Timestamp endTime;
		try{
			startTime = Timestamp.valueOf(stime);
			endTime = Timestamp.valueOf(etime);
			String sql = "insert into Exam(examName,startTime,endTime,whenLong,subjectCounts) values(?,?,?,?,?)";
			PreparedStatement insert = con.prepareStatement(sql);
			insert.setString(1, name);
			insert.setTimestamp(2, startTime);
			insert.setTimestamp(3, endTime);
			insert.setInt(4, whenLong);
			insert.setInt(5, counts);
			insert.execute();
		}
		catch(Exception e){
			return e.getMessage();
		}
		return "OK";
	}
	public String addExamData(String name,String Data){ //将创建的考试的题目存入数据库
		Matcher m = Pattern.compile("[^#{1}/*{1}#{1}]+").matcher(Data);
		try{
			String sql = "insert into ExamDataBank(examName,data) values(?,?)";
			PreparedStatement insert = con.prepareStatement(sql);
			while(m.find()){
				insert.setString(1, name);
				insert.setString(2, m.group());
				insert.execute();
			}
			Statement sta = con.createStatement();
			sta.execute("delete from RecentExam");
			sta.execute("insert into RecentExam values('"+name+"')");
		}
		catch(Exception e){
			return e.getMessage();
		}
		return "OK";
	}
	public String createEnvironment(String sql){ //为每位考生在其账户数据库下创建本次考试的环境，添加数据等;
		String[] data = sql.split(";");
		String[] user;
		String str = "";
		Statement sta;
		int i;
		try{
			sta = con.createStatement();
			ResultSet rs = sta.executeQuery("select User from User");
			while(rs.next()) str =str+rs.getString(1)+"#";
			user = str.split("#");
		}
		catch(Exception ex){
			return ex.getMessage();
		}
		String msg="";
		for(i=0;i<user.length;i++){
			try{
				sta.addBatch("use "+user[i]);
				for(int j=0;j<data.length;j++) sta.addBatch(data[j]);
				sta.executeBatch();
				sta.execute("use websql");
			}
			catch(Exception e){
				return msg = msg + e.getMessage();
			}
		}
		return "OK";
	}
	
	
	public String createPractise(String name,String time){
		Timestamp end;
		try{
			end = Timestamp.valueOf(time);
			PreparedStatement insert = con.prepareStatement("insert into Part(partName,endTime) values(?,?)");
			insert.setString(1, name);
			insert.setTimestamp(2, end);
			insert.execute();
		}
		catch(Exception e){
			return e.getMessage();
		}
		return "OK";
	}
	public String addPractiseData(String name,String Data){ //将创建的练习作业的题目存入数据库
		Matcher m = Pattern.compile("[^#{1}/*{1}#{1}]+").matcher(Data);
		try{
			PreparedStatement insert = con.prepareStatement("insert into PartDataBank(partName,data) values(?,?)");
			while(m.find()){
				insert.setString(1, name);
				insert.setString(2, m.group());
				insert.execute();
			}
		}
		catch(Exception e){
			return e.getMessage();
		}
		return "OK";
	}
}
